package com.first.computerstore.controller;

import com.first.computerstore.entity.Order;
import com.first.computerstore.query.OrderQuery;
import com.first.computerstore.service.OrderService;
import com.first.computerstore.utils.JsonResult;
import com.first.computerstore.vo.OrderVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 订单模块的controller层
 */
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {

    @Resource
    private OrderService orderService;



    /**
     * 根据用户id和订单状态查询订单列表
     * @param session
     * @param status
     * @return
     */
    @RequestMapping("/uidOrderVo")
    public JsonResult<List<OrderVo>> queryOrdersByUidAndStatus(HttpSession session,
                                                       Integer status){
        /* 获取参数 */
        Integer uid = getUserIdFromSession(session);
        OrderQuery query = new OrderQuery();
        query.setUid(uid);
        query.setStatus(status);

        /* 调用service层方法 */
        List<OrderVo> orderList = orderService.queryOrdersByUidAndStatus(query);

        /* 设置返回对象 */
        return  new JsonResult<>(200,orderList);

    }

    /**
     * 根据订单id更新订单状态
     * 修改订单状态：
     * 参数：订单id status uid
     * 修改字段
     *
     * @param oid
     * @param status
     * @return
     */
    @RequestMapping("/updateStatus")
    public JsonResult updateOrderStatusByOid(HttpSession session,
                                             Integer oid,Integer status){

        Order order = new Order();
        order.setOid(oid);
        order.setStatus(status);

        orderService.updateOrderStatusByOid(order);

        return new JsonResult(200);
    }

    /**
     * ”订单详情“ 订单+商品+地址：
     * 1、状态信息： 订单号： ， 当前状态：
     * 2、地址详情：收货人姓名：， 邮政编码：， 联系电话：， 收货地址：
     * 3、物流信息：
     * 4、商品详情：订单号：， 下单时间： ， 收货人
     *
     * @param oid
     * @return
     */
    @RequestMapping("queryOrderVo")
    public JsonResult<List<OrderVo>> queryOrderVo(Integer oid){

        List<OrderVo> orderVo = orderService.queryOrderVo(oid);

        return  new JsonResult<>(200,orderVo);
    }


    /**
     * 根据订单id查询订单对象
     * @param oid
     * @return
     */
    @RequestMapping("queryOrder")
    public JsonResult<Order> queryOrderByOid(Integer oid){
       Order order = orderService.queryOrderByOid(oid);
       return new JsonResult<>(200,order);
    }

    /**
     * 创建订单
     * @param aid
     * @param totalPrice
     * @param session
     * @return
     */
    @PostMapping("/createOrder")
    public JsonResult<Order> createOrder(Integer aid,Long totalPrice,HttpSession session) {
        Integer uid = getUserIdFromSession(session);
        String userName = getUsernameFromSession(session);

        Order order = orderService.insertOrder(aid,totalPrice,uid,userName);
        return new JsonResult<>(200,order);
    }

    /**
     * 创建订单商品
     * @param oid
     * @param cid
     * @param num
     * @param session
     * @return
     */
    @PostMapping("/createOrderItem")
    public JsonResult<Void> createOrderItem(Integer oid,Integer cid,Integer pid,Integer num,HttpSession session) {
        String  userName = getUsernameFromSession(session);
        if (pid == null){
            orderService.insertOrderItem(oid,cid,num,userName);
        }else {
            orderService.insertOrderItemFromProductHtml(oid,pid,num,userName);
        }

        return new JsonResult<>(OK);
    }

    }


